<template>
	<view class="content b-t">
		<view class="list b-b" v-for="(item, index) in addressList" :key="index" @click="checkAddress(item)">
			<view class="wrapper">
				<view class="address-box">
					<text v-if="item.default" class="tag">默认</text>
					<text class="address">{{item.address}}</text>
				</view>
				<view class="u-box">
					<text class="name">{{item.name}}</text>
					<text class="mobile">{{item.mobile}}</text>
				</view>

			</view>
			<!-- <text class="yticon icon-bianji" @click.stop="addAddress('edit', item)"></text> -->
			<img src="../../static/address/edit.png" @click.stop="addAddress('edit', item)" class="edit" />
			<img src="../../static/address/del.png" @click="delAddress(item)" class="delt" />
			
			
		</view>
		<text style="display:block;padding: 16upx 30upx 10upx;lihe-height: 1.6;color: #fa436a;font-size: 24upx;">
			重要：添加和修改地址回调仅增加了一条数据做演示，实际开发中将回调改为请求后端接口刷新一下列表即可
		</text>

		<button class="add-btn" @click="addAddress('add')">新增地址</button>
	</view>
</template>

<script>
	import {
		getAddress,
		delAddress
	} from '@/config/api/address.js';
    import { mapState } from 'vuex';
	export default {
		data() {
			return {
				source: 0,
				addressList: [
					
				]
			}
		},
		computed: {
		    ...mapState(['userInfo'])
		  },
		onLoad(option) {
			console.log(option.source);
			this.source = option.source;
		},
		mounted() {
			this.loadAddress()
		},
		methods: {
			//获取地址列表
			async loadAddress() {
				try {
					const userId = this.userInfo.id;//获取用户id
					const res = await getAddress({
						userId
					})
					if (res && res.data && typeof res.data === 'object' && 'code' in res.data && res.data.code ===
						200) {
						this.addressList = res.data.data
						console.log(JSON.stringify(this.addressList, null, 2));
					} else {
						console.error('请求失败:', res?.data?.msg || '未知错误');
					}
				} catch (error) {
					console.error('接口异常:', error)
					this.$message.error('数据加载失败')
				}
			},


			//选择地址
			checkAddress(item) {
				if (this.source == 1) {
					//this.$api.prePage()获取上一页实例，在App.vue定义
					this.$api.prePage().addressData = item;
					this.$api.prePage().isAddress = true;
					uni.navigateBack()
				}
			},
			addAddress(type, item) {
				uni.navigateTo({
					url: `/pages/address/addressManage?type=${type}&data=${JSON.stringify(item)}`
				})
			},
			async delAddress(item) {
				uni.showModal({
					title: '确认删除',
					content: '是否确认删除该地址？',
					confirmText: '确认',
					cancelText: '取消',
					success: async (res) => {
						if (res.confirm) {
							try {
								const userId = this.userInfo.id;
								let dtsAddress = {
									id: item.id,
									name: item.name,
									userId: item.userId,
									areaId: item.areaId,
									cityId: item.cityId,
									provinceId: item.provinceId,
									mobile: item.mobile,
								};
								console.log(dtsAddress);
								const delRes = await delAddress(dtsAddress)
								if (delRes && delRes.data && typeof delRes.data === 'object' && 'code' in
									delRes.data && delRes.data.code === 200) {
									await this.loadAddress();
									uni.showToast({
										title: '删除成功',
										icon: 'success'
									})
								} else {
									console.error('请求失败:', delRes?.data?.msg || '未知错误');
								}
							} catch (error) {
								console.error('接口异常:', error)
								this.$message.error('数据加载失败')
							}
						}
					}
				});
			},
			//添加或修改成功之后回调
			refreshList(data, type) {
				//添加或修改后事件，这里直接在最前面添加了一条数据，实际应用中直接刷新地址列表即可
				this.addressList.unshift(data);

				console.log(data, type);
			}
		}
	}
</script>

<style lang='scss'>
	page {
		padding-bottom: 120upx;
	}

	.content {
		position: relative;
	}

	.list {
		display: flex;
		align-items: center;
		padding: 20upx 30upx;
		;
		background: #fff;
		position: relative;
	}

	.wrapper {
		display: flex;
		flex-direction: column;
		flex: 1;
	}

	.address-box {
		display: flex;
		align-items: center;

		.tag {
			font-size: 24upx;
			color: $base-color;
			margin-right: 10upx;
			background: #fffafb;
			border: 1px solid #ffb4c7;
			border-radius: 4upx;
			padding: 4upx 10upx;
			line-height: 1;
		}

		.address {
			font-size: 30upx;
			color: $font-color-dark;
		}
	}

	.u-box {
		font-size: 28upx;
		color: $font-color-light;
		margin-top: 16upx;

		.name {
			margin-right: 30upx;
		}
	}

	.icon-bianji {
		display: flex;
		align-items: center;
		height: 80upx;
		font-size: 40upx;
		color: $font-color-light;
		padding-left: 30upx;
		padding-right: 15px;
	}
	.edit{
		width: 26px;
		height: 26px;
		margin-right: 15px;
	}
	.delt {
		width: 30px;
		height: 30px;
	}
	.add-btn {
		position: fixed;
		left: 30upx;
		right: 30upx;
		bottom: 16upx;
		z-index: 95;
		display: flex;
		align-items: center;
		justify-content: center;
		width: 690upx;
		height: 80upx;
		font-size: 32upx;
		color: #fff;
		background-color: $base-color;
		border-radius: 10upx;
		box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4);
	}
</style>